package servlet;

import bean.Category;
import bean.Product;
import bean.ProductImage;
import util.ImageUtil;
import util.Page;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

public class ProductServlet extends BaseBackServlet {
    @Override
    public String add(HttpServletRequest request, HttpServletResponse response, Page page) {
        int cid = Integer.parseInt(request.getParameter("cid"));
        String name = request.getParameter("product_name");
        String title = request.getParameter("product_subtitle");
        float oprice = Float.parseFloat(request.getParameter("product_oprice"));
        float nprice = Float.parseFloat(request.getParameter("product_nprice"));
        int stock = Integer.parseInt(request.getParameter("product_stock"));
        Product product = new Product();
        Category category = categoryDAO.get(cid);
        product.setCategory(category);
        product.setName(name);
        product.setCreateDate(new Date());
        product.setOrignalPrice(oprice);
        product.setPromotePrice(nprice);
        product.setStock(stock);
        product.setSubTitle(title);
        productDAO.add(product);
//        System.out.println(property);
        return "@admin_product_list?cid="+cid;
    }

    @Override
    public String delete(HttpServletRequest request, HttpServletResponse respone, Page page) {
        int id = Integer.parseInt(request.getParameter("pid"));
        int cid = Integer.parseInt(request.getParameter("cid"));
        boolean result = false;
        String str = "";
        Product product = productDAO.get(id);
        if(product.getProductImages()!=null)
        {
            str = "@admin_product_list?delete=no&cid="+cid;
        }
        else
        {
            productDAO.delete(id);
            str = "@admin_product_list?cid="+cid;
        }
        return str;
    }

    @Override
    public String edit(HttpServletRequest request, HttpServletResponse response, Page page) {
        int cid = Integer.parseInt(request.getParameter("cid"));
        int id = Integer.parseInt(request.getParameter("pid"));
        Product product = productDAO.get(id);
        Category category = product.getCategory();
        request.setAttribute("category",category);
        request.setAttribute("product",product);
        request.setAttribute("page", page);
        return "jsp/admin/editProduct.jsp";
    }

    @Override
    public String update(HttpServletRequest request, HttpServletResponse response, Page page) {
        int id = Integer.parseInt(request.getParameter("product_id"));
        String name = request.getParameter("product_Nname");
        String title = request.getParameter("product_Nsubtitle");
        float oprice = Float.parseFloat(request.getParameter("product_Noprice"));
        float nprice = Float.parseFloat(request.getParameter("product_Nnprice"));
        int stock = Integer.parseInt(request.getParameter("product_Nstock"));
        Product product = productDAO.get(id);
        Category category = product.getCategory();
        product.setCategory(category);
        product.setName(name);
        product.setCreateDate(null);
        product.setOrignalPrice(oprice);
        product.setPromotePrice(nprice);
        product.setStock(stock);
        product.setSubTitle(title);
        product.setFirstProductImage(null);
        productDAO.update(product);
//        System.out.println(property);
        return "@admin_product_list?cid="+category.getId();
    }

    @Override
    public String list(HttpServletRequest request, HttpServletResponse response, Page page) {
        int start = page.getStart();
        int count = page.getCount();
        int cid = Integer.parseInt(request.getParameter("cid"));
        int total = productDAO.getTotal(cid);
        page.setTotal(total);
        Category category = categoryDAO.get(cid);
//        System.out.println(start+"--"+count+"--"+total);
        List<Product> productss = productDAO.list(cid);
        for (Product product:
             productss) {
            productDAO.setFirstProductImage(product);
        }
        List<Product> products = productDAO.list(cid,start,count);
        page.setParam("&cid="+category.getId());
        request.setAttribute("products", products);
        request.setAttribute("category",category);
        request.setAttribute("page", page);
        return "jsp/admin/listProduct.jsp";
    }
}
